package com.guigu.financial.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.financial.enums.BorrowInfoStatusEnum;
import com.guigu.financial.mapper.BorrowInfoMapper;
import com.guigu.financial.pojo.BorrowInfo;
import com.guigu.financial.pojo.vo.BorrowInfoApprovalVO;
import com.guigu.financial.service.BorrowInfoService;
import com.guigu.financial.service.DictService;
import com.guigu.financial.service.LendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BorrowInfoServiceImpl implements BorrowInfoService {
    @Autowired
    private BorrowInfoMapper borrowInfoMapper;
    @Autowired
    private DictService dictService;
    @Autowired
    private LendService lendService;

    @Override
    public void approval(BorrowInfoApprovalVO borrowInfoApprovalVO) {
        Long id = borrowInfoApprovalVO.getId();
        BorrowInfo borrowInfo = borrowInfoMapper.selectByPrimaryKey(id);
        borrowInfo.setStatus(borrowInfoApprovalVO.getStatus());
        borrowInfoMapper.updateByPrimaryKeySelective(borrowInfo);

        // 如果通过审核，则产生标记记录
        if (borrowInfoApprovalVO.getStatus().intValue() == BorrowInfoStatusEnum.CHECK_OK.getStatus().intValue()){
            lendService.createLend(borrowInfoApprovalVO,borrowInfo);
        }
    }

    @Override
    public PageInfo findAll(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<BorrowInfo> borrowInfos = borrowInfoMapper.findAll();
        PageInfo pageInfo = new PageInfo(borrowInfos);
        return pageInfo;
    }
}
